package com.javasea.qrprint.util;

import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *@ClassName UiUtil
 *@Description TODO
 *@Author longxiaonan@163.com
 *@Date 2022/3/25 0025 15:59
 */
public class UiUtil {
    private static ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
    public static void scheduleInit(Stage primaryStage) {
        scheduler.schedule(()->{
            System.out.println("Schedule setup ...");
            setup(primaryStage);
            alreadySetup = true;
        }, 3, TimeUnit.SECONDS);
    }

    public static void popupStartupConfirm(Stage primaryStage) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("启动完毕");
        alert.setHeaderText("提示");
        alert.setContentText("公司简介打印启动完成");
        alert.showAndWait().ifPresent(rs -> {
            if (rs == ButtonType.OK) {
                setup(primaryStage);
                alreadySetup = true;
            }
        });
    }

    private static Boolean alreadySetup = false;
    private static void setup(Stage primaryStage) {
        if (alreadySetup) {
            return;
        }
        Platform.runLater(() -> {
            System.setProperty("java.awt.headless", "false");
            TrayOperatorUtil.hideToSystemTray();
            Platform.setImplicitExit(false); // 关闭时不退出
//            primaryStage.setIconified(true); // 托盘化
//            primaryStage.close(); // 关闭(未退出)
        });
    }
}
